package main

import (
	"fmt"
	"server_cluster/common/logger"
	"server_cluster/gate/db"
	"server_cluster/gate/gameLogic"
	"server_cluster/gate/gate_config"
	"server_cluster/gate/sharemem"
	"time"
)

func game_sync() {
	err := db.InitDB()
	if err != nil {
		panic(fmt.Sprintf("init db fail. %v", err))
	}

	logger.Info("game sync start...")
	myshm := sharemem.Myshm
	myshm.Status_sync = 1

	i := 0
	logger.Info("game sync loop...")
	for myshm.Status_sync == 1 {
		i++
		if gate_config.CONF.DEV_ENV && i%60 == 0 {
			err = gameLogic.LoadConfigData()
			if err != nil {
				logger.Error("reload config error: %v", err)
				break
			}
		}
		time.Sleep(1 * time.Second)
	}

	myshm.Status_sync = 0
	logger.Info("game sync stop.")
}

func main() {
	gate_config.InitConfig()
	logger.InitLogger("sync", gate_config.GetConfig().LOG_LEVEL)
	defer logger.CloseLogger()
	shmid := sharemem.Get_myshm(&(sharemem.Myshm))
	if shmid <= 0 {
		logger.Error("Get myshm fail.")
		return
	}
	myshm := sharemem.Myshm
	if myshm.Status_sync != 0 {
		logger.Error("Server sync is not init.")
		return
	}

	game_sync()
}
